package main;
import java.util.concurrent.LinkedBlockingQueue;

public class Graph {
	private LinkedBlockingQueue<Task> tasks = new LinkedBlockingQueue<Task>();

	public LinkedBlockingQueue<Task> getTasks() {
		return tasks;
	}
	
	public void addTask(Task task) {
		tasks.add(task);
	}
	
	public void addPredecessor(Task task, Task predecessor) {
		task.addPredecessor(predecessor);
	}
	
	public boolean formsCycle(String id) {
		for (Task task : tasks) {
			if (task.getId().equals(id)) {
				return true;
			}
		}
		return false;
	}
	
	public Task getTaskById(String id) {
		Task task = null;
		for (Task t : tasks) {
			if (t.getId().equals(id)) {
				task = t;
				break;
			}
		}
		return task;
	}

	@Override
	public String toString() {
		StringBuffer g = new StringBuffer();
		for (Task task : tasks) {
			g.append("{");
			g.append(task);
			g.append(":");
			for (Task t : task.getPredecessors()) {
				g.append(t);
			}
			g.append("}");
		}
		return g.toString();
	}
	
	public void print() {
		System.out.println ("==========================");
		System.out.println(this.toString());
		System.out.println ("==========================");
	}
}
